/**
 * created by 小卷毛, 2021-07-09
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.model.*
import com.rzico.sbl.ui.report.other.ReportService
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import org.json.JSONObject

/**
 * 描 述：报货系统操作相关
 * 作 者：小卷毛  2021-07-09 10:55
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class ReportViewModel : CommonViewModel() {

    val mBuyEvent = MutableLiveData(0)     //采购单
    val mOrderEvent = MutableLiveData(0)   //订单
    val mDeliverEvent = MutableLiveData(0) //运单

    val mOrderJsonEvent = MutableLiveData(JSONObject())   //司机首页订单
    val mDeliverJsonEvent = MutableLiveData(JSONObject()) //司机首页运单

    private fun driverIconsMode1() = arrayListOf(
        ManagerDivider("商品"),
        ManagerContent("商品", R.mipmap.icon_report01),
        ManagerContent("采购单", R.mipmap.icon_report02),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("订单"),
        ManagerContent("订单", R.mipmap.icon_report14),
        ManagerContent("运单", R.mipmap.icon_report15),
        ManagerContent("水站", R.mipmap.icon_report16),
        ManagerContent("出入库", R.mipmap.icon_report27),
        ManagerDivider("优惠"),
        ManagerContent("卡券", R.mipmap.icon_report03),
        ManagerContent("活动区", R.mipmap.icon_report04),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("统计"),
        ManagerContent("商品统计", R.mipmap.icon_report05),
        ManagerContent("水站统计", R.mipmap.icon_report28),
        ManagerContent("库存统计", R.mipmap.icon_report30),
        ManagerContent("代金券统计", R.mipmap.icon_report31),
        ManagerDivider()
    )

    private fun driverIconsMode2() = arrayListOf(
        ManagerDivider("商品"),
        ManagerContent("商品", R.mipmap.icon_report01),
        ManagerContent("采购单", R.mipmap.icon_report02),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("订单"),
        ManagerContent("运单", R.mipmap.icon_report15),
        ManagerContent("水站", R.mipmap.icon_report16),
        ManagerContent("出入库", R.mipmap.icon_report27),
        ManagerContent(),
        ManagerDivider("统计"),
        ManagerContent("商品统计", R.mipmap.icon_report05),
        ManagerContent("水站统计", R.mipmap.icon_report28),
        ManagerContent("库存统计", R.mipmap.icon_report30),
        ManagerContent(),
        ManagerDivider()
    )

    private fun stationIconsMode1() = arrayListOf(
        ManagerDivider("商品"),
        ManagerContent("商品", R.mipmap.icon_report01),
        ManagerContent("采购单", R.mipmap.icon_report02),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("优惠"),
        ManagerContent("卡券", R.mipmap.icon_report03),
        ManagerContent("活动区", R.mipmap.icon_report04),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("统计"),
        ManagerContent("商品统计", R.mipmap.icon_report05),
        ManagerContent("水站统计", R.mipmap.icon_report28),
        ManagerContent("库存统计", R.mipmap.icon_report30),
        ManagerContent(),
        ManagerDivider()
    )

    private fun stationIconsMode2() = arrayListOf(
        ManagerDivider("商品"),
        ManagerContent("商品", R.mipmap.icon_report01),
        ManagerContent("采购单", R.mipmap.icon_report02),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("统计"),
        ManagerContent("商品统计", R.mipmap.icon_report05),
        ManagerContent("水站统计", R.mipmap.icon_report28),
        ManagerContent("库存统计", R.mipmap.icon_report30),
        ManagerContent(),
        ManagerDivider()
    )

    private fun drivereListMode1() = arrayListOf(
        ManagerContent("出入库", R.mipmap.icon_report_n_01),
        ManagerContent("水站", R.mipmap.icon_report_m_05),
        ManagerContent("商品统计", R.mipmap.icon_report_n_02),
        ManagerContent("水站统计", R.mipmap.icon_report_n_03),
        ManagerContent("库存统计", R.mipmap.icon_report_n_15),
        ManagerContent("代金券统计", R.mipmap.icon_report_n_16),
    )

    private fun drivereListMode2() = arrayListOf(
        ManagerContent("出入库", R.mipmap.icon_report_n_01),
        ManagerContent("水站", R.mipmap.icon_report_m_05),
        ManagerContent("商品统计", R.mipmap.icon_report_n_02),
        ManagerContent("水站统计", R.mipmap.icon_report_n_03),
        ManagerContent("库存统计", R.mipmap.icon_report_n_15)
    )

    // 报货首页
    fun getIconList(serviceId: String, status: String) =
        when (serviceId) {
            in ReportService.MODE_DRIVER ->
                when (status) {
                    "1" -> driverIconsMode1()
                    else -> stationIconsMode1()
                }
            else ->
                when (status) {
                    "1" -> driverIconsMode2()
                    else -> stationIconsMode2()
                }

        }

    // 司机首页
    fun getDriverList(serviceId: String) =
        when (serviceId) {
            in ReportService.MODE_DRIVER -> drivereListMode1()
            else -> drivereListMode2()
        }

    /**
     * 采购单、订单数量
     */
    fun getCount(type: String) =
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = ReportUrl.getBaseUrl().reportCut,
            params = arrayOf("type" to type),
            onSuccess = {
                JSONObject(it).apply {
                    mOrderJsonEvent.value = this

                    when (type) {
                        "0" -> mBuyEvent.value = optNotNull("unpaid").intValue
                        "1" -> mOrderEvent.value = optNotNull("unpaid").intValue
                    }
                }
            }
        )

    /**
     * 运单数量
     */
    fun getShipCount() =
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = ReportUrl.getBaseUrl().reportSCut,
            onSuccess = {
                JSONObject(it).apply {
                    mDeliverJsonEvent.value = this

                    mDeliverEvent.value =
                        optNotNull("confirmed").intValue
                }
            }
        )

    /**
     * 商品分类
     */
    fun productCat() =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(BaseUrl.productCat)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    /**
     * 被修改的商品分类
     */
    fun productMCat(orderSN: String) =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(ReportUrl.getBaseUrl().productMCat)
            .params(getParams("sn" to orderSN))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    /**
     * 报货商品列表
     */
    fun reportGoods(
        page: Int,
        orderSn: String,
        kindId: String,
        keyWord: String,
        onSuccess: (ArrayList<ReportGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<ReportGoodsData>>>(ReportUrl.getBaseUrl().reportGoods)
            .isMultipart(true)
            .params(
                getParams(
                    "sn" to orderSn,
                    "productCategoryId" to kindId,
                    "keyword" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 库存明细
     */
    fun goodsSum(
        page: Int,
        goodsId: String
    ) = OkGo.get<BaseResponse<ResponseModel<ReportCouponData>>>(ReportUrl.getBaseUrl().reportGSum)
        .params(
            getParams(
                "id" to goodsId,
                "pageStart" to page * 20,
                "pageSize" to 20
            )
        )
        .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportCouponData>>>() {})
        .adapt(FlowableBody())
        .compose(applyFlowableSchedulers())
        .map { it.data.data }

    /**
     * 商品置顶
     */
    fun goodsTop(itemId: String) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().reportTop,
            params = getParams("id" to itemId)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 创建订单
     * BaseUrl.reportCrt
     * BaseUrl.reportCrt2
     */
    fun goodsCreate(url: String, orderSN: String, json: String) =
        RxSingleHelper.requestString(
            url = url,
            params = getParams(
                "sn" to orderSN,
                "body" to json
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 立即下单
     * BaseUrl.reportSub
     * BaseUrl.reportSub2
     */
    fun goodsSubmit(url: String, vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = url,
            isMultipart = true,
            params = getParams(*params),
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 订单详情
     */
    fun orderDetail(orderSN: String) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().reportDet,
            params = getParams("sn" to orderSN)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    /**
     * 活动区列表
     */
    fun promoteList(
        page: Int,
        orderSn: String,
        kindId: String,
        keyWord: String,
        onSuccess: (ArrayList<ReportGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<ReportGoodsData>>>(ReportUrl.getBaseUrl().promoteList)
            .isMultipart(true)
            .params(
                getParams(
                    "sn" to orderSn,
                    "productCategoryId" to kindId,
                    "keyword" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 优惠券
     */
    fun reportCpn() =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().reportCpn,
            method = HttpMethod.GET
        )
            .compose(applySchedulers())
            .doOnSubscribe { }
            .doFinally { }

    /**
     * 优惠券明细
     */
    fun reportSum(
        page: Int,
        type: String,
        shopId: String
    ) = OkGo.get<BaseResponse<ResponseModel<ReportCouponData>>>(ReportUrl.getBaseUrl().reportSum)
        .params(
            getParams(
                "type" to type,
                "enterpriseId" to shopId,
                "pageStart" to page * 20,
                "pageSize" to 20
            )
        )
        .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportCouponData>>>() {})
        .adapt(FlowableBody())
        .compose(applyFlowableSchedulers())
        .map { it.data.data }

    /**
     * 确认订单
     */
    fun confirmOrder(orderSN: String) =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().reportCfm,
            params = getParams("sn" to orderSN)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}